import hashlib
import re

import scrapy
from ..items import MyFileItem


class Spider (scrapy.Spider):
    name = 'idn_bphn'
    allowed_domains = ['bphn.jdihn.go.id']
    # 印度尼西亚法律发展署法律文件地址
    start_urls = ['https://bphn.jdihn.go.id/dokumen/index?DokumenSearch%5Btipe_dokumen%5D=1&DokumenSearch%5Bjenis_peraturan%5D=&DokumenSearch%5Bnomor_peraturan%5D=&DokumenSearch%5Btahun_terbit%5D=&DokumenSearch%5Bstatus_terakhir%5D=']
    id = 0

    # 成员列表页
    def parse(self, response):
        # 获得成员列表
        trs = response.xpath('//*[@id="w0"]/div[@class="item"]')
        for tr in trs:
            # 详细信息网站
            showUrlS = tr.xpath('.//div[@class="card-body no-padding-tb"]/p/a/@href')
            if len(showUrlS):
                showa = tr.xpath('.//div[@class="card-body no-padding-tb"]/p/a')[0]
                yield response.follow(showa, callback=self.showurl, dont_filter=True)
        nextaUrlS = response.xpath('//*[@id="w0"]/ul/li[@class="next"]/a/@href')
        if len(nextaUrlS):
            nexta = response.xpath('//*[@id="w0"]/ul/li[@class="next"]/a')[0]
            yield  response.follow(nexta, callback=self.parse, dont_filter=True)

    # 解析详细信息页
    def showurl(self, response):
        detailUrl = response.url
        # 法律状态
        StatuS = response.xpath('//div[@class="side-bar"]/div[2]/ul/li[2]//text()')
        Statu = ''
        if len(StatuS):
            for StatuSl in StatuS:
                Statu = Statu + ' ' + StatuSl.get().strip()
            Statu = Statu.strip()
        if Statu == "Berlaku":
            # 效力级别
            SORTES = response.xpath('//div[@class="side-bar"]/div[1]/ul/li[2]//text()')
            SORTE = ''
            if len(SORTES):
                for SORTESl in SORTES:
                    SORTE = SORTE + ' ' + SORTESl.get().strip()
                SORTE = SORTE.strip()
            # 标题
            LegalNameS = response.xpath('//div[@class="content"]/div[1]/div[@class="post-title"]//text()')
            LegalName = ''
            if len(LegalNameS):
                for LegalNameSl in LegalNameS:
                    LegalName = LegalName + ' ' + LegalNameSl.get().strip()
                LegalName = LegalName.strip()  # 去除两头的空格
            # 发文日期
            PublishDateS = response.xpath('//div[@class="content"]/div[2]/div[2]/span//text()')
            PublishDate = ''
            if len(PublishDateS):
                PublishDate = PublishDateS[0].get().strip()
            # 生效日期
            EffectiveDateS = response.xpath('//div[@class="content"]/div[2]/div[3]/span//text()')
            EffectiveDate = ''
            if len(EffectiveDateS):
                EffectiveDate = EffectiveDateS[0].get().strip()
            # 内容分类
            SORTFS = response.xpath('//div[@class="content"]/div[2]/div[6]/span//text()')
            SORTF = ''
            if len(SORTFS):
                SORTF = SORTFS[0].get().strip()
            downloadUrlS = response.xpath('//div[@class="side-bar"]/div[3]/ul/a/@href')
            if len(downloadUrlS):
                a = response.xpath('//div[@class="side-bar"]/div[3]/ul/a')[0]
                yield response.follow(a, callback=self.detailed, dont_filter=True,
                                      meta={'LegalName': LegalName, 'PublishDate': PublishDate, 'EffectiveDate': EffectiveDate,
                                            'SORTE': SORTE, 'SORTF': SORTF, 'detailUrl': detailUrl})

    # 解析详情页
    def detailed(self, response):
        item = MyFileItem()
        if response.status == 200:
            # 法律详细网址
            downloadUrl = response.url
            LegalName = response.meta['LegalName']
            PublishDate = response.meta['PublishDate']
            EffectiveDate = response.meta['EffectiveDate']
            SORTE = response.meta['SORTE']
            SORTF = response.meta['SORTF']
            detailUrl = response.meta['detailUrl']
            # 下载文件名
            fina = ''
            if len(detailUrl) > 0:
                fina = 'f' + str(hashlib.md5(detailUrl.encode('utf-8')).hexdigest())
            # 唯一ID
            systemid = str(hashlib.md5(detailUrl.encode('utf-8')).hexdigest())

            item['file_urls'] = ''
            item['country'] = 'Indonesia'
            item['website'] = 'bphn'
            item['modular'] = 'law'
            item['title'] = ''
            item['ext'] = 'pdf'
            item['fina'] = fina
            item['chapNo'] = ''
            item['detailUrl'] = ''
            item['downloadUrl'] = downloadUrl
            item['htmls'] = ''
            item['htmlUrl'] = ''
            item['abstract'] = ''
            item['abstractUrl'] = ''

            item['LegalName'] = LegalName
            item['Organizaation'] = ''
            item['PublishDate'] = PublishDate
            item['EffectiveDate'] = EffectiveDate
            item['SortA'] = 'LAWCOUNTRYYDNXY'
            item['SortB'] = 'LANGUAGEYNIY'
            item['SortC'] = ''
            item['SortD'] = ''
            item['SORTE'] = SORTE
            item['SORTF'] = SORTF
            item['Keyword'] = ''
            item['SORTG'] = ''
            item['ChapNo'] = ''
            item['Articles'] = ''
            item['Chapter'] = ''
            item['Section'] = ''
            item['SYS_FLD_DIGITFILENAME'] = fina
            item['FileUrl'] = ''
            item['DownLoadUrl'] = detailUrl
            item['DownLoadWebNameC'] = '印度尼西亚国家法律发展署'
            item['DownLoadWebNameE'] = 'BADAN PEMBINAAN HUKUM NASIONAL'
            item['SYSID'] = systemid
            item['Website'] = 'BADAN PEMBINAAN HUKUM NASIONAL'
            item['Isconversion'] = '0'
            item['Revisionmark'] = ''

            yield item
